package icu.wdtt.controller;


import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.NotRoleException;
import icu.wdtt.Utils.ResultUtils;
import icu.wdtt.enumModel.HttpRequestMessage;
import icu.wdtt.exception.FilesException;
import icu.wdtt.exception.UserException;
import icu.wdtt.model.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.ui.Model;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.UnsatisfiedServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class ExceptionController {
   @Autowired
   private ResultUtils resultUtils;

   //处理自定义的User接口异常
    @ExceptionHandler({UserException.class})
    public Result userException(UserException ue){
        return resultUtils.fail(ue.getMessage(),400);
    }
   //处理自定义的文件接口异常
    @ExceptionHandler({FilesException.class})
    public Result FilesException(FilesException fe ){
        return resultUtils.fail(fe.getMessage(),400);
    }

    //处理参数格式异常
    @ExceptionHandler(IllegalStateException.class)
    //更改状态码 这里为500
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Result badRequestException(IllegalStateException exception){
       return resultUtils.fail("参数格式异常或未携带指定参数！",HttpStatus.INTERNAL_SERVER_ERROR.value());
    }

    // 参数缺失异常处理
    @ExceptionHandler({MissingServletRequestParameterException.class})
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public Result badRequestException(MissingServletRequestParameterException exception) {
        return resultUtils.fail( "缺少必填参数！",HttpStatus.BAD_REQUEST.value());
    }

    //未携带某参数的异常处理器
    @ExceptionHandler({UnsatisfiedServletRequestParameterException.class})
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public Result badRequestException(Exception exception) {
        //获取没有携带的参数信息
        String exMsg = exception.getMessage();
        int frist = exMsg.indexOf('"');
        int last = exMsg.indexOf('"',frist+1);
        String msg = exMsg.substring(frist+1,last);
        //返回参数
        return resultUtils.fail( "未携带指定参数:"+msg,HttpStatus.BAD_REQUEST.value());
    }

    //未登陆异常的异常处理器
    @ExceptionHandler({NotLoginException.class})
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public Result NotLoginException(NotLoginException exception) {
        return resultUtils.fail( HttpRequestMessage.FAIL_NOT_LOGIN);
    }

    //权限不足异常处理
    @ExceptionHandler({NotPermissionException.class})
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public Result notPermission(NotPermissionException exception) {
        return resultUtils.fail(HttpRequestMessage.FAIL_NO_PERMISSION);
    }
    //角色权限不足异常处理
    @ExceptionHandler({NotRoleException.class})
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public Result notRoleException(NotRoleException exception){
        return resultUtils.fail(HttpRequestMessage.FAIL_NO_ROLE);
    }

    //最高级异常处理
    @ExceptionHandler(value = Exception.class)
    public Result handleException(Exception e){
        return resultUtils.fail(HttpRequestMessage.INTERNAL_ERROR);
    }
}
